import pandas as pd
import numpy as np
from datetime import datetime, timedelta


class Mktdata(object):
    mkt_data = None
    mkt_hour = None
    mkt_day = None
    mkt_period = None
    fmt = '%Y-%m-%d %H:%M:%S%z'

    def __init__(self, file_path, period, begin: datetime, end: datetime):
        self.period = period
        raw_data = pd.read_csv(file_path)
        raw_data.dropna(inplace=True)
        raw_data = raw_data[raw_data["volume"] != 0]

        raw_min = pd.DataFrame(raw_data[["datetime", "open", "high", "low", "close", "volume"]])

        self.fmt = '%Y-%m-%d %H:%M:%S%z' if '+' in raw_min.loc[0, 'datetime'] else '%Y-%m-%d %H:%M:%S'

        raw_min.index = pd.to_datetime(raw_min.datetime, format=self.fmt).dt.tz_localize(None)
        # raw_min.index = pd.to_datetime(raw_min.datetime, format=self.fmt)

        # raw_min.set_index('datetime', inplace=True, drop=False)

        self.mkt_data = raw_min[begin:end]
        self.mkt_hour = self.to_period("1h")
        self.mkt_day = self.to_period("1d")
        self.mkt_period = self.to_period(period)
        print(self.period)



    def to_period(self, period):
        if period == "1T":
            return self.mkt_data
        df = self.mkt_data.copy()
        # df.index = pd.to_datetime(df.datetime, format=self.fmt)

        df = df[["datetime", "open", "high", "low", "close", "volume"]]

        res_data = df.resample(period).last()
        res_data['datetime'] = df['datetime'].resample(period).first()
        res_data['endtime'] = df['datetime'].resample(period).last()

        res_data['open'] = df['open'].resample(period).first()
        res_data['close'] = df['close'].resample(period).last()
        res_data['high'] = df['high'].resample(period).max()
        res_data['low'] = df['low'].resample(period).min()
        res_data['volume'] = df['volume'].resample(period).sum()

        res_data.dropna(inplace=True)
        res_data = res_data[["datetime", 'endtime', "open", "high", "low", "close", "volume"]]
        print(f"时间戳转换完成, {period}")

        return res_data


class Symbol(object):
    def __init__(self, name: str, data: Mktdata):
        self.name = name
        self.data = data
        self.daily_settle = None

    def get_parameters(self):
        pass

    def get_fees(self):
        pass
